%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%The objective function
function nu = myfunc(input)

% display of the input(Ds,Be,delta,Bd,Bcs)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%Définition des paramètres d'entrée
Ds=input(1);
Be=input(2);
delta=input(3);
Bd=input(4);
Bcs=input(5);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Donnies du cahier des charges
C=20; omega=721*pi/30;omegamax=1442*pi/30;

%paramètres d'entrée
Lm=45e-3;

% Propriités des matériaux
%%%%calcul de Pj
kr=0.5; rocu=1.72e-8; kfoi=0.95; Pm=15;
%%%%calcul de Pf
qt=2.5;ft=50;dt=7850;Bt=1.5;mua=1.05;kfui=0.8;Br=1.045;

% Propriitis des matiriaux pour la thermique
alphaa=-5e-4;alphacu=3.8e-3;Text=50; h=10;

% Paramhtres d'entrie fixis
e=0.8e-3; Udc=120; Bcr=1.2; rrs=1.11; p=6; Ne=3/2*p;

%global alpha,alphaa

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%calcul de Pj=2*Rph*I^2;%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%calcul de I
kv=omegamax/omega;
E=Udc/2/kv;
I=C*omega/2/E;
%%%%%%%%%%%%%%%%%%
%%% calcul de Rph
n=4*E/Be/Ds/Lm/omega; % Equations dimensionnantes

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Equations dimensionnantes
beta=pi/p;
alpha=pi/p;
alphai=alpha/5;
li=Be/Bd*alphai*Ds/2;
ld=Be/Bd*alpha*Ds/2;
eb=(Be/Bd*(alpha/2-asin(ld/Ds))*Ds/2)+Ds/2*(1-cos(asin(ld/Ds)));

%%%%%%%%%
Senc=3/2*n*I/delta/kr;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% calcul hauteur dent
% calcul discriminant pour hauteur dent
a=pi;
b=-(2*pi*(Ds/2-eb)-Ne*(li+ld));
c=Senc;
discr=b^2-4*a*c;
hd=(-b-sqrt(abs(discr)))/2/a;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Rtb=((ld-li)/4+(Ds/2-eb-hd/2)*pi/2/Ne); % calcul hauteur dent
Lds=(Lm/kfoi+pi*Rtb); % calcul hauteur dent
Ba=Be*alpha*Ds/2/beta/(Ds/2+e)/rrs/kfui;
hcr=1/2*Ba/Bcr*beta*(Ds/2+e);
Ltot=(Lm/kfoi+2*((Ds/2-eb-hd/2)*pi/Ne-li/2));
%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%calcul de Pf%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%calcul hauteur dent
hc=(eb/cos(alpha/2)-Ds/2*(1/cos(alpha/2)-1));
hi=(Ds/2*(1-cos(alphai/2))+hc*cos(alphai/2));
Mds=dt*Ne*((ld+li)*hd+Ds/2*(alphai*(hi+hc)/2+alpha*(eb+hc)/2))*Lm;
hcs=1/2*Bd/Bcs*ld;
Mcs=dt*pi*hcs*(-hcs+2*(Ds/2-eb-hd))*Lm;
f=p*omega/2/pi;
Pf=qt*((f/ft)^1.5)*(Mcs*((Bcs/Bt)^2)+Mds*((Bd/Bt)^2));
%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%Fin calcul de Pf

% systhme d'iquations implicites
Ta=100; Tcu=110;
while abs(Ta-Tcu)>1e-6
    Ta=Tcu;
    ha=-mua*Be*e/(Ba-Br*(1+alphaa*Ta));
    Dext=Ds+2*(e+ha+hcr);
    Rph=rocu*(1+alphacu*Tcu)*n/2*Lds*delta/I;
    Pj=2*Rph*I^2;
    Sext=pi/2*(Dext^2)+pi*Dext*Ltot;
    Tcu=Text+(Pj+Pf+Pm)/h/Sext;
end
% fin systhme d'iquations implicites
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%Fin calcul de Pj


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%Calcul du rendement
nu = (C*omega-Pm)/(C*omega+Pj+Pf);
nu=1-nu;

%global fct=fct+1

